package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.DictTypeDTO;
import com.andy.blog.admin.dto.SysDictDataDTO;
import com.andy.blog.admin.service.SysDictBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.dal.vo.DictTypeVO;
import com.andy.blog.dal.vo.SysDictDataVO;
import com.andy.blog.dal.vo.SysDictTypeVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by andy on 2023/7/20 15:52
 */
@RestController
@RequestMapping("/system/dict")
public class SysDictController {

    @Resource
    private SysDictBusService sysDictBusService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/data/list")
    public GeneralResponseResult<PageUtilsDTO<SysDictDataDTO>> selectListByPage(SysDictDataVO sysDictDataVO) {
        return GeneralResponseResult.success(sysDictBusService.selectDictDataByPage(sysDictDataVO));
    }

    /**
     * 导出
     */
    @Log(title = "字典数据", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/data/export")
    public void export(HttpServletResponse response, SysDictDataVO sysDictDataVO) {
        List<SysDictDataDTO> list = sysDictBusService.selectDictDataByCondition(sysDictDataVO);
        EasyExcelUtils.writeStyleExcel(response, list, "字典数据", SysDictDataDTO.class);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/data/{id}")
    public GeneralResponseResult<SysDictDataDTO> getDictDataInfo(@PathVariable Long id) {
        return GeneralResponseResult.success(sysDictBusService.selectById(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/data/type/{dictType}")
    public GeneralResponseResult<List<SysDictDataDTO>> dictType(@PathVariable String dictType) {
        return GeneralResponseResult.success(sysDictBusService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/data")
    public GeneralResponseResult<Void> add(@Validated @RequestBody SysDictDataVO sysDictDataVO) {
        sysDictBusService.insertDictData(sysDictDataVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/data")
    public GeneralResponseResult<Void> edit(@Validated @RequestBody SysDictDataVO sysDictDataVO) {
        sysDictBusService.updateDictData(sysDictDataVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/data/{dictCodes}")
    public GeneralResponseResult<Void> removeDcitData(@PathVariable Long[] dictCodes) {
        sysDictBusService.deleteDictDataByIds(dictCodes);
        return GeneralResponseResult.success();
    }


    /**
     * 根据条件分页查询字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/type/selectListByPage")
    public GeneralResponseResult<PageUtilsDTO<DictTypeDTO>> selectListByPage(DictTypeVO dictTypeVO) {
        return GeneralResponseResult.success(sysDictBusService.selectDateTypeByPage(dictTypeVO));
    }

    /**
     * 导出
     */
    @Log(title = "字典类型", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/type/export")
    public void export(HttpServletResponse response, DictTypeVO dictTypeVO) {
        List<DictTypeDTO> list = sysDictBusService.export(dictTypeVO);
        EasyExcelUtils.writeStyleExcel(response, list, "字典类型", DictTypeDTO.class);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/type/{dictId}")
    public GeneralResponseResult<DictTypeDTO> getInfo(@PathVariable Long dictId) {
        return GeneralResponseResult.success(sysDictBusService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/type")
    public GeneralResponseResult<Void> add(@Validated @RequestBody SysDictTypeVO sysDictTypeVO) {
        sysDictBusService.insertDictType(sysDictTypeVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/type")
    public GeneralResponseResult<String> edit(@Validated @RequestBody SysDictTypeVO sysDictTypeVO) {
        sysDictBusService.update(sysDictTypeVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/type/{dictIds}")
    public GeneralResponseResult<Void> remove(@PathVariable Long[] dictIds) {
        sysDictBusService.deleteByIds(dictIds);
        return GeneralResponseResult.success();
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/type/refreshCache")
    public GeneralResponseResult<Void> refreshCache() {
        sysDictBusService.resetDictCache();
        return GeneralResponseResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/type/optionselect")
    public GeneralResponseResult<List<DictTypeDTO>> optionselect() {
        List<DictTypeDTO> dictTypeDTOS = sysDictBusService.selectDictTypeAll();
        return GeneralResponseResult.success(dictTypeDTOS);
    }
}
